package yxy.game.pm2.active.command.limitgiftpackage;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actlimitpackage;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_限时礼包_Area;
import yxy.game.pm2.active.module.ActiveModule_限时礼包_Person;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.LimitGiftPackage;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActLimitGiftPackageRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;
import java.util.Map;

@MessageType(proto = Command.ActLimitGiftPackage, classtype = ActiveMessage.ActLimitGiftPackageRequest.class)
public class CMD_Active_限时礼包_Info extends Command<ActLimitGiftPackageRequest> {

	private static final Logger logger = Logger.create(CMD_Active_限时礼包_Info.class);
	
	@Override
	public void execute(MessageSource messageSource, ActLimitGiftPackageRequest request) throws Exception {
		String playercode = messageSource.getTag();
		
		ActiveMessage.ActLimitGiftPackageResponse.Builder response = ActiveMessage.ActLimitGiftPackageResponse.newBuilder();
		
		PlayerBean pb = PlayerBean.get(playercode);
		if(pb == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		ActiveModule_限时礼包_Area areaam = ActiveModule_限时礼包_Area.get(pb.getServerId());
		if(areaam == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(!areaam.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		ActiveModule_限时礼包_Person am = ActiveModule_限时礼包_Person.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		response.setDay(am.getDays());
		
		Map<String, actlimitpackage> map = ActDataCenter.getAllData(actlimitpackage.class, playercode);
		
		for(LimitGiftPackage lgp : am.getNewlgps()) {
			ActiveMessage.LimitGiftPackageInfo.Builder build = ActiveMessage.LimitGiftPackageInfo.newBuilder();
			build.setId(lgp.getId());
			build.setGoodsid(lgp.getGoodsid());
			build.setLimit(lgp.getLimit());
			build.setLocknum(lgp.getLockorder().size());
			build.setBuynum(lgp.getBuy());
			
			int canget = lgp.getBuy() + lgp.getLockorder().size() >= lgp.getLimit() ? DrawStatus.cannotget.getStatus() :
				DrawStatus.canget.getStatus();
			build.setCanget(canget);
			
			actlimitpackage alp = map.get(String.valueOf(lgp.getId()));
			if(alp == null) {
				continue;
			}
			
			Collection<Currency> curs = Currency.create(alp.getRewardtype(), alp.getRewardid(), alp.getRewardnum());
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
			build.addAllCurs(items);
			response.addLgpinfos(build.build());
		}
		
		send(messageSource, response, Result.Success);
	}
	
	private void send(MessageSource messageSource, ActiveMessage.ActLimitGiftPackageResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}


}
